package com.yongqi.duocai.controller;


import com.google.gson.Gson;
import com.yongqi.duocai.entity.Address;
import com.yongqi.duocai.entity.Invoice;
import com.yongqi.duocai.entity.param.AddressParam;
import com.yongqi.duocai.exception.BusinessException;
import com.yongqi.duocai.service.AddressService;
import com.yongqi.duocai.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 夏亮辉
 * @since 2021-06-22
 */

@RestController
@RequestMapping("/duocai/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("/query")
    @ApiOperation("查询当前用户的地址信息")
    public JsonResult queryByUserId(Integer userId) throws InterruptedException {
        JsonResult jsonResult = new JsonResult();
        List<Address> addresses = addressService.queryByUserId(userId);
        if(addresses.size() > 0){
            jsonResult.setCode(200);
            jsonResult.setData(addresses);
        }else if (addresses.size() <= 0){
            jsonResult.setCode(200);
            jsonResult.setMessage("很遗憾！您没有地址信息。");
        }
        return jsonResult;
    }

    @GetMapping("/query/default")
    @ApiOperation("查询当前用户的默认地址信息")
    public JsonResult queryDefault(Integer userId){
        JsonResult jsonResult = new JsonResult();
        List<Address> addresses = addressService.queryByUserId(userId);
        Address defaultAddress = null;
        for (Address a : addresses){
            if(a.getIsDefault()){
                defaultAddress = a;
            }
        }
        if(defaultAddress != null){
            jsonResult.setCode(200);
            jsonResult.setData(defaultAddress);
        }else if (addresses.size() <= 0){
            jsonResult.setCode(200);
            jsonResult.setMessage("很遗憾！您没有默认地址信息。");
        }
        return jsonResult;
    }

    @PutMapping("/update")
    @ApiOperation("修改地址信息")
    public JsonResult update(Integer id, AddressParam param){
        JsonResult jsonResult = new JsonResult();
        Address condition = new Address();
        condition.setId(id);
        BeanUtils.copyProperties(param,condition);
        boolean res = addressService.updateById(condition);
        if(res){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功");
        }else {
            jsonResult.setMessage("修改失败");
        }
        return jsonResult;
    }

    @DeleteMapping("/del")
    @ApiOperation("删除地址信息")
    public JsonResult delete(Integer id){
        JsonResult jsonResult = new JsonResult();
        boolean res = addressService.removeById(id);
        if(res){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功");
        }else {
            jsonResult.setMessage("删除失败");
        }
        return jsonResult;
    }

    @PostMapping("/add")
    @ApiOperation("新增地址信息")
    public JsonResult add(AddressParam param){
        JsonResult jsonResult = new JsonResult();
        Address target = new Address();
        BeanUtils.copyProperties(param,target);
        List<Address> condition = addressService.queryOne(target);
        if(condition.size() > 0){
            jsonResult.setCode(200);
            jsonResult.setMessage("当前信息已存在，请勿重复添加！");
            return jsonResult;
        }
        // 根据id查找用户地址
        List<Address> addresses = addressService.queryByUserId(param.getUserId());
        if(addresses.size() == 0){
            // 当前用户第一次添加地址
            target.setIsDefault(true);
        }else {
            target.setIsDefault(false);
        }
        // 对参数进行查询防止重复添加
//        addressService.get
        boolean res = addressService.save(target);
        if(res){
            jsonResult.setCode(200);
            jsonResult.setMessage("新增成功");
        }else {
            jsonResult.setMessage("新增失败");
        }
        return jsonResult;
    }

    @PutMapping("/update/default")
    @ApiOperation("将某个地址信息设置为默认地址")
    public JsonResult updateDefault(Integer useId,Integer id){
        JsonResult jsonResult = new JsonResult();
        Address address = addressService.queryDefault(useId);

        if(address.getIsDefault()){
            Map<String,Object> code = new HashMap<>();
            code.put("isDefault", address.getIsDefault());
            Gson gson = new Gson();
            String str = gson.toJson(code);
            jsonResult.setCode(200);
            jsonResult.setData(str);
            jsonResult.setMessage("设置失败");
            return jsonResult;
        }

        address.setIsDefault(false);
        Address condition = addressService.getById(id);
        condition.setIsDefault(true);
        boolean re = addressService.updateById(address);
        boolean res = addressService.updateById(condition);
        if(res && res){
            jsonResult.setCode(200);
            jsonResult.setMessage("设置成功！");
        }else {
            jsonResult.setMessage("设置失败");
        }
        return jsonResult;

    }
}

